<!DOCTYPE html>
<html lang="zh-CN">
    <head>
  <!-- 元数据 -->
  <meta charset="utf-8">
  <link rel="icon" href="/blog/images/logo.png">
  
  <title>【Java 8 新特性】时间类：Duration 和 Period | 溪岚花的博客</title>
  
  <meta name="author" content="林家隆" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="robots" content="index,follow" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta name="format-detection" content="telphone=no, email=no" />
  
    <meta name="keywords" content="Java" />
  
  <meta name="description" content="本章介绍 java.time 包下两个跟时间长度相关的类： Duration 和 Period。 Duration：表示两个时间点之间的时间长度，可以表示纳秒、秒、分钟、小时等等。 Period：表示两个日期之间的时间跨度，精确到年、月、日单位。 DurationDuration 类由两个变量 seconds 和 nanos 组成，适用于计算时间间隔，比如计算两个时间点之间相差的秒数、毫秒数等等，">
<meta property="og:type" content="article">
<meta property="og:title" content="【Java 8 新特性】时间类：Duration 和 Period">
<meta property="og:url" content="https://lin316.gitee.io/blog/2023/05/10/Java8/time/Duration_Period/index.html">
<meta property="og:site_name" content="溪岚花的博客">
<meta property="og:description" content="本章介绍 java.time 包下两个跟时间长度相关的类： Duration 和 Period。 Duration：表示两个时间点之间的时间长度，可以表示纳秒、秒、分钟、小时等等。 Period：表示两个日期之间的时间跨度，精确到年、月、日单位。 DurationDuration 类由两个变量 seconds 和 nanos 组成，适用于计算时间间隔，比如计算两个时间点之间相差的秒数、毫秒数等等，">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://lin316.gitee.io/blog/blog/images/logo.png">
<meta property="article:published_time" content="2023-05-09T16:00:00.000Z">
<meta property="article:modified_time" content="2025-11-20T11:35:22.171Z">
<meta property="article:author" content="林家隆">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://lin316.gitee.io/blog/blog/images/logo.png">
  
  <!-- 站点验证相关 -->
  
    
    
    
  
  <!-- 样式表文件 -->
  <link rel="stylesheet" id="kratos-css" href="/blog/css/kratosr.min.css" media="all"></script>
  
    <link rel="stylesheet" id="darkmode-css" href="/blog/css/kr-color-dark.min.css" media="(prefers-color-scheme: dark)"></script>
    <script src="/blog/js/kr-dark.min.js"></script>
  
  
    <link rel="stylesheet" id="highlight-css" href="/blog/css/highlight/night-eighties.min.css" media="all"></script>
  
  <link rel="stylesheet" id="fontawe-css" href="/blog/vendors/font-awesome@4.7.0/css/font-awesome.min.css" media="all"></script>
  <link rel="stylesheet" id="nprogress-css" href="/blog/vendors/nprogress@0.2.0/nprogress.css" media="all"></script>
  
  
  
    <link rel="stylesheet" href="/blog/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css"></script>
  
  <!-- 不得不预先加载的一些JS文件 -->
  <script src="/blog/vendors/jquery@3.6.0/dist/jquery.min.js"></script>
  
  
  <style>
    
    
  </style>
  
<meta name="generator" content="Hexo 6.3.0"></head>


    <body class="custom-background">
        <div id="kratos-wrapper">
    <div id="kratos-page">
        <div id="kratos-header">
            <header id="kratos-desktop-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="nav-header">
                        <nav id="kratos-menu-wrap">
                            <ul id="kratos-primary-menu" class="sf-menu">
                                
                                    
                                        <li>
                                            
                                                <a href="/blog/">
                                            
                                                
                                                    <i class="fa fa-home"></i>
                                                
                                                首页
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/archives/">
                                            
                                                
                                                    <i class="fa fa-file"></i>
                                                
                                                归档
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/tags/">
                                            
                                                
                                                    <i class="fa fa-tag"></i>
                                                
                                                标签
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/categories/">
                                            
                                                
                                                    <i class="fa fa-folder"></i>
                                                
                                                目录
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/search/">
                                            
                                                
                                                    <i class="fa fa-search"></i>
                                                
                                                搜索
                                            </a>
                                            
                                        </li>
                                    
                                
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <header id="kratos-mobile-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="color-logo"><a href="/blog/">溪岚花的博客</a></div>
                    <div class="nav-toggle">
                        <a class="kratos-nav-toggle js-kratos-nav-toggle">
                            <i></i>
                        </a>
                    </div>
                </div>
            </header>
        </div>
        <div class="kratos-start kratos-hero-2">
            <!-- <div class="kratos-overlay"></div> -->
            <div class="kratos-cover kratos-cover-2 text-center">
                <div class="desc desc2 animate-box">
                    <a href="/blog/">
                        <h2 style="opacity: 0.2;">溪岚花的博客</h2> <br />
                        <span></span>
                    </a>
                </div>
            </div>
        </div>

        <div id="kratos-blog-post">
            <div class="container">
                <div id="main" class="row">
                    

        

            <section class="col-md-8">

        

            <article itemscope itemtype="https://schema.org/Article">
    
    <link itemprop="mainEntityOfPage" href="https://lin316.gitee.io/blog/2023/05/10/Java8/time/Duration_Period/">
    <div class="kratos-hentry kratos-post-inner clearfix">
        <header class="kratos-entry-header">
            
                <h1 class="kratos-entry-title text-center" itemprop="name headline">【Java 8 新特性】时间类：Duration 和 Period</h1>
            
            
            <ul class="kratos-post-meta text-center">
                <li><time datetime="2023-05-09T16:00:00.000Z" itemprop="datePublished"><i class="fa fa-calendar"></i> 2023-05-10</time></li>
                <li itemprop="author" itemscope itemtype="https://schema.org/Person">
                    <i class="fa fa-user"></i> 作者 <span itemprop="name">林家隆</span>
                </li>
                
                    <li>
                        <i class="fa fa-edit"></i> 
                        
                        
                            10.67K
                        
                        字
                    </li>
                
                
            </ul>
        </header>
        <div class="kratos-post-content">
            
            <div id="expire-alert" class="alert alert-warning hidden" role="alert">
                <div class="icon"><i class="fa fa-warning"></i></div>
                <div class="text"><p>本文最后编辑于 <time datetime="1763638522171"></time> 前，其中的内容可能需要更新。</p></div>
            </div>
            
            
            
                <div class="kratos-post-inner-toc toc-div-class" >
                    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Duration"><span class="toc-number">1.</span> <span class="toc-text">Duration</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AC%E5%85%B1%E5%B8%B8%E9%87%8F"><span class="toc-number">1.1.</span> <span class="toc-text">公共常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95"><span class="toc-number">1.2.</span> <span class="toc-text">静态方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#parse-CharSequence-text"><span class="toc-number">1.2.1.</span> <span class="toc-text">parse(CharSequence text)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#between-Temporal-startInclusive-Temporal-endExclusive"><span class="toc-number">1.2.2.</span> <span class="toc-text">between(Temporal startInclusive, Temporal endExclusive)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-long-amount-TemporalUnit-unit"><span class="toc-number">1.2.3.</span> <span class="toc-text">of(long amount, TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofDays-long-days"><span class="toc-number">1.2.4.</span> <span class="toc-text">ofDays(long days)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofHours-long-hours"><span class="toc-number">1.2.5.</span> <span class="toc-text">ofHours(long hours)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofMinutes-long-minutes"><span class="toc-number">1.2.6.</span> <span class="toc-text">ofMinutes(long minutes)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofSeconds-long-seconds"><span class="toc-number">1.2.7.</span> <span class="toc-text">ofSeconds(long seconds)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofSeconds-long-seconds-long-nanoAdjustment"><span class="toc-number">1.2.8.</span> <span class="toc-text">ofSeconds(long seconds, long nanoAdjustment)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofMillis-long-millis"><span class="toc-number">1.2.9.</span> <span class="toc-text">ofMillis(long millis)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofNanos-long-nanos"><span class="toc-number">1.2.10.</span> <span class="toc-text">ofNanos(long nanos)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%88%90%E5%91%98%E6%96%B9%E6%B3%95"><span class="toc-number">1.3.</span> <span class="toc-text">成员方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#abs"><span class="toc-number">1.3.1.</span> <span class="toc-text">abs()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#negated"><span class="toc-number">1.3.2.</span> <span class="toc-text">negated()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#addTo-Temporal-temporal"><span class="toc-number">1.3.3.</span> <span class="toc-text">addTo(Temporal temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#subtractFrom-Temporal-temporal"><span class="toc-number">1.3.4.</span> <span class="toc-text">subtractFrom(Temporal temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#compareTo-Duration-otherDuration"><span class="toc-number">1.3.5.</span> <span class="toc-text">compareTo(Duration otherDuration)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#multipliedBy-long-multiplicand"><span class="toc-number">1.3.6.</span> <span class="toc-text">multipliedBy(long multiplicand)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dividedBy-long-divisor"><span class="toc-number">1.3.7.</span> <span class="toc-text">dividedBy(long divisor)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getUnits"><span class="toc-number">1.3.8.</span> <span class="toc-text">getUnits()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%AA%E6%BC%94%E7%A4%BA%E6%96%B9%E6%B3%95"><span class="toc-number">1.3.9.</span> <span class="toc-text">未演示方法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Period"><span class="toc-number">2.</span> <span class="toc-text">Period</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AC%E5%85%B1%E5%B8%B8%E9%87%8F-1"><span class="toc-number">2.1.</span> <span class="toc-text">公共常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95-1"><span class="toc-number">2.2.</span> <span class="toc-text">静态方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#parse-CharSequence-text-1"><span class="toc-number">2.2.1.</span> <span class="toc-text">parse(CharSequence text)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#between-LocalDate-startDateInclusive-LocalDate-endDateExclusive"><span class="toc-number">2.2.2.</span> <span class="toc-text">between(LocalDate startDateInclusive, LocalDate endDateExclusive)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-int-years-int-months-int-days"><span class="toc-number">2.2.3.</span> <span class="toc-text">of(int years, int months, int days)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofDays-int-days"><span class="toc-number">2.2.4.</span> <span class="toc-text">ofDays(int days)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofMonths-int-months"><span class="toc-number">2.2.5.</span> <span class="toc-text">ofMonths(int months)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofWeeks-int-weeks"><span class="toc-number">2.2.6.</span> <span class="toc-text">ofWeeks(int weeks)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofYears-int-years"><span class="toc-number">2.2.7.</span> <span class="toc-text">ofYears(int years)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%88%90%E5%91%98%E6%96%B9%E6%B3%95-1"><span class="toc-number">2.3.</span> <span class="toc-text">成员方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#addTo-Temporal-temporal-1"><span class="toc-number">2.3.1.</span> <span class="toc-text">addTo(Temporal temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#subtractFrom-Temporal-temporal-1"><span class="toc-number">2.3.2.</span> <span class="toc-text">subtractFrom(Temporal temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getUnits-1"><span class="toc-number">2.3.3.</span> <span class="toc-text">getUnits()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isNegative"><span class="toc-number">2.3.4.</span> <span class="toc-text">isNegative()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isZero"><span class="toc-number">2.3.5.</span> <span class="toc-text">isZero()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#multipliedBy-int-scalar"><span class="toc-number">2.3.6.</span> <span class="toc-text">multipliedBy(int scalar)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#negated-1"><span class="toc-number">2.3.7.</span> <span class="toc-text">negated()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#normalized"><span class="toc-number">2.3.8.</span> <span class="toc-text">normalized()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#toTotalMonths"><span class="toc-number">2.3.9.</span> <span class="toc-text">toTotalMonths()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%AA%E6%BC%94%E7%A4%BA%E6%96%B9%E6%B3%95-1"><span class="toc-number">2.3.10.</span> <span class="toc-text">未演示方法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE"><span class="toc-number">3.</span> <span class="toc-text">参考文献</span></a></li></ol>
                </div>
            
            <hr />
            <div itemprop="articleBody"><p>本章介绍 <code>java.time</code> 包下两个跟时间长度相关的类： <code>Duration</code> 和 <code>Period</code>。</p>
<p><code>Duration</code>：表示两个时间点之间的时间长度，可以表示纳秒、秒、分钟、小时等等。</p>
<p><code>Period</code>：表示两个日期之间的时间跨度，精确到年、月、日单位。</p>
<h2 id="Duration"><a href="#Duration" class="headerlink" title="Duration"></a>Duration</h2><p><code>Duration</code> 类由两个变量 <code>seconds</code> 和 <code>nanos</code> 组成，适用于计算时间间隔，比如计算两个时间点之间相差的秒数、毫秒数等等，可以表示时间的长度精度到纳秒级别。</p>
<h3 id="公共常量"><a href="#公共常量" class="headerlink" title="公共常量"></a>公共常量</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* ZERO：时间长度为零的常数。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span>&#123;</span><br><span class="line">    System.out.println(Duration.ZERO); <span class="comment">// 输出 PT0S</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="静态方法"><a href="#静态方法" class="headerlink" title="静态方法"></a>静态方法</h3><h4 id="parse-CharSequence-text"><a href="#parse-CharSequence-text" class="headerlink" title="parse(CharSequence text)"></a>parse(CharSequence text)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* parse(CharSequence text)</span></span><br><span class="line"><span class="comment">* 作用：将字符串解析成duration对象。字符串格式如下：</span></span><br><span class="line"><span class="comment">*     &quot;PT20.34S&quot;  -- 解析成 20.34秒</span></span><br><span class="line"><span class="comment">*     &quot;PT15M&quot;     -- 解析成 15分钟</span></span><br><span class="line"><span class="comment">*     &quot;PT10H&quot;     -- 解析成 10小时</span></span><br><span class="line"><span class="comment">*     &quot;P2D&quot;       -- 解析成 2天</span></span><br><span class="line"><span class="comment">*     &quot;P2DT3H4M&quot;  -- 解析成 2天3小时4分钟</span></span><br><span class="line"><span class="comment">*     &quot;P-6H3M&quot;    -- 解析成 负6小时正3分钟</span></span><br><span class="line"><span class="comment">*     &quot;-P6H3M&quot;    -- 解析成 负6小时负3分钟</span></span><br><span class="line"><span class="comment">*     &quot;-P-6H+3M&quot;  -- 解析成 正6小时负3分钟</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span>&#123;</span><br><span class="line">    System.out.println(Duration.parse(<span class="string">&quot;PT1S&quot;</span>)); <span class="comment">// 输出 PT1S</span></span><br><span class="line">    System.out.println(Duration.parse(<span class="string">&quot;PT1M&quot;</span>)); <span class="comment">// 输出 PT1M</span></span><br><span class="line">    System.out.println(Duration.parse(<span class="string">&quot;PT1H&quot;</span>)); <span class="comment">// 输出 PT1H</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="between-Temporal-startInclusive-Temporal-endExclusive"><a href="#between-Temporal-startInclusive-Temporal-endExclusive" class="headerlink" title="between(Temporal startInclusive, Temporal endExclusive)"></a>between(Temporal startInclusive, Temporal endExclusive)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* between(Temporal startInclusive, Temporal endExclusive)</span></span><br><span class="line"><span class="comment">* 作用：计算两个时间点之间相差的秒数或纳秒数。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">LocalDateTime</span> <span class="variable">startDateTime</span> <span class="operator">=</span> LocalDateTime.parse(<span class="string">&quot;2023-04-02T08:00:00.001&quot;</span>);</span><br><span class="line">    <span class="type">LocalDateTime</span> <span class="variable">endDateTime</span> <span class="operator">=</span> LocalDateTime.parse(<span class="string">&quot;2023-04-02T09:00:00.002&quot;</span>);</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration01</span> <span class="operator">=</span> Duration.between(startDateTime, endDateTime);</span><br><span class="line">    System.out.println(duration01); <span class="comment">// 输出 PT1H0.001S</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="of-long-amount-TemporalUnit-unit"><a href="#of-long-amount-TemporalUnit-unit" class="headerlink" title="of(long amount, TemporalUnit unit)"></a>of(long amount, TemporalUnit unit)</h4><h4 id="ofDays-long-days"><a href="#ofDays-long-days" class="headerlink" title="ofDays(long days)"></a>ofDays(long days)</h4><h4 id="ofHours-long-hours"><a href="#ofHours-long-hours" class="headerlink" title="ofHours(long hours)"></a>ofHours(long hours)</h4><h4 id="ofMinutes-long-minutes"><a href="#ofMinutes-long-minutes" class="headerlink" title="ofMinutes(long minutes)"></a>ofMinutes(long minutes)</h4><h4 id="ofSeconds-long-seconds"><a href="#ofSeconds-long-seconds" class="headerlink" title="ofSeconds(long seconds)"></a>ofSeconds(long seconds)</h4><h4 id="ofSeconds-long-seconds-long-nanoAdjustment"><a href="#ofSeconds-long-seconds-long-nanoAdjustment" class="headerlink" title="ofSeconds(long seconds, long nanoAdjustment)"></a>ofSeconds(long seconds, long nanoAdjustment)</h4><h4 id="ofMillis-long-millis"><a href="#ofMillis-long-millis" class="headerlink" title="ofMillis(long millis)"></a>ofMillis(long millis)</h4><h4 id="ofNanos-long-nanos"><a href="#ofNanos-long-nanos" class="headerlink" title="ofNanos(long nanos)"></a>ofNanos(long nanos)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* of(long amount, TemporalUnit unit)</span></span><br><span class="line"><span class="comment">* ofDays(long days)</span></span><br><span class="line"><span class="comment">* ofHours(long hours)</span></span><br><span class="line"><span class="comment">* ofMinutes(long minutes)</span></span><br><span class="line"><span class="comment">* ofSeconds(long seconds)</span></span><br><span class="line"><span class="comment">* ofSeconds(long seconds, long nanoAdjustment)</span></span><br><span class="line"><span class="comment">* ofMillis(long millis)</span></span><br><span class="line"><span class="comment">* ofNanos(long nanos)</span></span><br><span class="line"><span class="comment">* 作用：指定时间长度创建duration对象。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test03</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration01</span> <span class="operator">=</span> Duration.of(<span class="number">1</span>, ChronoUnit.SECONDS);</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration02</span> <span class="operator">=</span> Duration.of(<span class="number">1</span>, ChronoUnit.MINUTES);</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration03</span> <span class="operator">=</span> Duration.of(<span class="number">1</span>, ChronoUnit.HOURS);</span><br><span class="line">    System.out.println(duration01); <span class="comment">// 输出 PT1S</span></span><br><span class="line">    System.out.println(duration02); <span class="comment">// 输出 PT1M</span></span><br><span class="line">    System.out.println(duration03); <span class="comment">// 输出 PT1H</span></span><br><span class="line"></span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration04</span> <span class="operator">=</span> Duration.ofSeconds(<span class="number">1</span>);</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration05</span> <span class="operator">=</span> Duration.ofMinutes(<span class="number">1</span>);</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration06</span> <span class="operator">=</span> Duration.ofHours(<span class="number">1</span>);</span><br><span class="line">    System.out.println(duration04); <span class="comment">// 输出 PT1S</span></span><br><span class="line">    System.out.println(duration05); <span class="comment">// 输出 PT1M</span></span><br><span class="line">    System.out.println(duration06); <span class="comment">// 输出 PT1H</span></span><br><span class="line"></span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration07</span> <span class="operator">=</span> Duration.ofSeconds(<span class="number">1</span>, <span class="number">15</span>);</span><br><span class="line">    System.out.println(duration07); <span class="comment">// 输出 PT1.000000015S</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="成员方法"><a href="#成员方法" class="headerlink" title="成员方法"></a>成员方法</h3><h4 id="abs"><a href="#abs" class="headerlink" title="abs()"></a>abs()</h4><h4 id="negated"><a href="#negated" class="headerlink" title="negated()"></a>negated()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* abs()</span></span><br><span class="line"><span class="comment">* 作用：返回时间长度的绝对值。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* negated()</span></span><br><span class="line"><span class="comment">* 作用：返回时间长度的相反数。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalDateTime</span> <span class="variable">startDateTime</span> <span class="operator">=</span> LocalDateTime.parse(<span class="string">&quot;2023-04-02T08:00:00&quot;</span>);</span><br><span class="line">    <span class="type">LocalDateTime</span> <span class="variable">endDateTime</span> <span class="operator">=</span> LocalDateTime.parse(<span class="string">&quot;2023-04-02T09:00:00&quot;</span>);</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration01</span> <span class="operator">=</span> Duration.between(endDateTime, startDateTime);</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration02</span> <span class="operator">=</span> Duration.between(startDateTime, endDateTime);</span><br><span class="line">    System.out.println(duration01.getSeconds()); <span class="comment">// 输出 -3600</span></span><br><span class="line">    System.out.println(duration01.abs().getSeconds()); <span class="comment">// 输出 3600</span></span><br><span class="line">    System.out.println(duration02.negated().getSeconds()); <span class="comment">// 输出 -3600</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="addTo-Temporal-temporal"><a href="#addTo-Temporal-temporal" class="headerlink" title="addTo(Temporal temporal)"></a>addTo(Temporal temporal)</h4><h4 id="subtractFrom-Temporal-temporal"><a href="#subtractFrom-Temporal-temporal" class="headerlink" title="subtractFrom(Temporal temporal)"></a>subtractFrom(Temporal temporal)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* addTo(Temporal temporal)</span></span><br><span class="line"><span class="comment">* subtractFrom(Temporal temporal)</span></span><br><span class="line"><span class="comment">* 作用：在指定的时间对象上 加上/减去 时间长度。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalDateTime</span> <span class="variable">dateTime01</span> <span class="operator">=</span> LocalDateTime.parse(<span class="string">&quot;2023-04-02T09:00:00&quot;</span>);</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration01</span> <span class="operator">=</span> Duration.ofHours(<span class="number">1</span>).plusMinutes(<span class="number">15</span>);</span><br><span class="line">    <span class="type">LocalDateTime</span> <span class="variable">dateTime02</span> <span class="operator">=</span> (LocalDateTime) duration01.addTo(dateTime01);</span><br><span class="line">    <span class="type">LocalDateTime</span> <span class="variable">dateTime03</span> <span class="operator">=</span> (LocalDateTime) duration01.subtractFrom(dateTime01);</span><br><span class="line">    System.out.println(dateTime01); <span class="comment">// 输出 2023-04-02T09:00</span></span><br><span class="line">    System.out.println(dateTime02); <span class="comment">// 输出 2023-04-02T10:15</span></span><br><span class="line">    System.out.println(dateTime03); <span class="comment">// 输出 2023-04-02T07:45</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="compareTo-Duration-otherDuration"><a href="#compareTo-Duration-otherDuration" class="headerlink" title="compareTo(Duration otherDuration)"></a>compareTo(Duration otherDuration)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* compareTo(Duration otherDuration)</span></span><br><span class="line"><span class="comment">* 作用：对比两个duration对象的大小。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test03</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration01</span> <span class="operator">=</span> Duration.ofHours(<span class="number">1</span>).plusMinutes(<span class="number">15</span>);</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration02</span> <span class="operator">=</span> Duration.ofHours(<span class="number">1</span>);</span><br><span class="line">    System.out.println(duration01.compareTo(duration02)); <span class="comment">// 输出 1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="multipliedBy-long-multiplicand"><a href="#multipliedBy-long-multiplicand" class="headerlink" title="multipliedBy(long multiplicand)"></a>multipliedBy(long multiplicand)</h4><h4 id="dividedBy-long-divisor"><a href="#dividedBy-long-divisor" class="headerlink" title="dividedBy(long divisor)"></a>dividedBy(long divisor)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* multipliedBy(long multiplicand)</span></span><br><span class="line"><span class="comment">* dividedBy(long divisor)</span></span><br><span class="line"><span class="comment">* 作用：将时间长度 扩大/缩小 指定倍数。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test04</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration01</span> <span class="operator">=</span> Duration.ofSeconds(<span class="number">100</span>);</span><br><span class="line">    System.out.println(duration01.getSeconds()); <span class="comment">// 输出 100</span></span><br><span class="line">    System.out.println(duration01.multipliedBy(<span class="number">10</span>).getSeconds()); <span class="comment">// 输出 1000</span></span><br><span class="line">    System.out.println(duration01.dividedBy(<span class="number">10</span>).getSeconds()); <span class="comment">// 输出 10</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="getUnits"><a href="#getUnits" class="headerlink" title="getUnits()"></a>getUnits()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* getUnits()</span></span><br><span class="line"><span class="comment">* 作用：获取duration对象支持的时间单位。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test05</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration01</span> <span class="operator">=</span> Duration.ofSeconds(<span class="number">45</span>, <span class="number">100</span>);</span><br><span class="line">    System.out.println(duration01.getUnits()); <span class="comment">// 输出 [Seconds, Nanos]</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="未演示方法"><a href="#未演示方法" class="headerlink" title="未演示方法"></a>未演示方法</h4><p>以下方法大多在 <code>LocalDate</code> 和 <code>LocalTime</code> 篇章演示过且作用类似，该篇章就不在赘述了。</p>
<ol>
<li>equals(Object otherDuration)</li>
<li>get(TemporalUnit unit)</li>
<li>getNano()</li>
<li>getSeconds()</li>
<li>hashCode()</li>
<li>minus(Duration duration)</li>
<li>minus(long amountToSubtract, TemporalUnit unit)</li>
<li>minusDays(long daysToSubtract)</li>
<li>minusHours(long hoursToSubtract)</li>
<li>minusMillis(long millisToSubtract)</li>
<li>minusMinutes(long minutesToSubtract)</li>
<li>minusNanos(long nanosToSubtract)</li>
<li>minusSeconds(long secondsToSubtract)</li>
<li>plus(Duration duration)</li>
<li>plus(long amountToAdd, TemporalUnit unit)</li>
<li>plusDays(long daysToAdd)</li>
<li>plusHours(long hoursToAdd)</li>
<li>plusMillis(long millisToAdd)</li>
<li>plusMinutes(long minutesToAdd)</li>
<li>plusNanos(long nanosToAdd)</li>
<li>plusSeconds(long secondsToAdd)</li>
<li>toString()</li>
<li>withNanos(int nanoOfSecond)</li>
<li>withSeconds(long seconds)</li>
</ol>
<br>

<h2 id="Period"><a href="#Period" class="headerlink" title="Period"></a>Period</h2><p><code>Period</code> 类由三个变量 <code>years</code>、<code>months</code> 和 <code>days</code> 组成，适用于更大的时间跨度，比如计算两个日期之间相差的年数、月数、天数。</p>
<h3 id="公共常量-1"><a href="#公共常量-1" class="headerlink" title="公共常量"></a>公共常量</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* ZERO：时间长度为零的常数。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span> &#123;</span><br><span class="line">    System.out.println(Period.ZERO); <span class="comment">// 输出 P0D</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="静态方法-1"><a href="#静态方法-1" class="headerlink" title="静态方法"></a>静态方法</h3><h4 id="parse-CharSequence-text-1"><a href="#parse-CharSequence-text-1" class="headerlink" title="parse(CharSequence text)"></a>parse(CharSequence text)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* parse(CharSequence text)</span></span><br><span class="line"><span class="comment">* 作用：将字符串解析成Period对象，字符串格式如下：</span></span><br><span class="line"><span class="comment">*      &quot;P2Y&quot;        解析成  2年</span></span><br><span class="line"><span class="comment">*      &quot;P3M&quot;        解析成  3个月</span></span><br><span class="line"><span class="comment">*      &quot;P4W&quot;        解析成  4个星期</span></span><br><span class="line"><span class="comment">*      &quot;P5D&quot;        解析成  5天</span></span><br><span class="line"><span class="comment">*      &quot;P1Y2M3D&quot;    解析成  1年2月3天</span></span><br><span class="line"><span class="comment">*      &quot;P1Y2M3W4D&quot;  解析成  1年2月3星期4天</span></span><br><span class="line"><span class="comment">*      &quot;P-1Y2M&quot;     解析成  负1年正2月</span></span><br><span class="line"><span class="comment">*      &quot;-P1Y2M&quot;     解析成  负1年负2月</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period01</span> <span class="operator">=</span> Period.parse(<span class="string">&quot;P1Y2M3D&quot;</span>);</span><br><span class="line">    System.out.println(period01.getYears()); <span class="comment">// 输出 1</span></span><br><span class="line">    System.out.println(period01.getMonths()); <span class="comment">// 输出 2</span></span><br><span class="line">    System.out.println(period01.getDays()); <span class="comment">// 输出 3</span></span><br><span class="line"></span><br><span class="line">    <span class="type">Period</span> <span class="variable">period02</span> <span class="operator">=</span> Period.parse(<span class="string">&quot;P1Y&quot;</span>);</span><br><span class="line">    System.out.println(period02.getYears()); <span class="comment">// 输出 1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="between-LocalDate-startDateInclusive-LocalDate-endDateExclusive"><a href="#between-LocalDate-startDateInclusive-LocalDate-endDateExclusive" class="headerlink" title="between(LocalDate startDateInclusive, LocalDate endDateExclusive)"></a>between(LocalDate startDateInclusive, LocalDate endDateExclusive)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* between(LocalDate startDateInclusive, LocalDate endDateExclusive)</span></span><br><span class="line"><span class="comment">* 作用：计算两个日期之间相差的年数、月数、天数。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span> &#123;</span><br><span class="line">    <span class="type">LocalDate</span> <span class="variable">startDate</span> <span class="operator">=</span> LocalDate.parse(<span class="string">&quot;2021-01-01&quot;</span>);</span><br><span class="line">    <span class="type">LocalDate</span> <span class="variable">endDate</span> <span class="operator">=</span> LocalDate.parse(<span class="string">&quot;2022-03-04&quot;</span>);</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period01</span> <span class="operator">=</span> Period.between(startDate, endDate);</span><br><span class="line">    System.out.println(period01.getYears()); <span class="comment">// 输出 1</span></span><br><span class="line">    System.out.println(period01.getMonths()); <span class="comment">// 输出 2</span></span><br><span class="line">    System.out.println(period01.getDays()); <span class="comment">// 输出 3</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="of-int-years-int-months-int-days"><a href="#of-int-years-int-months-int-days" class="headerlink" title="of(int years, int months, int days)"></a>of(int years, int months, int days)</h4><h4 id="ofDays-int-days"><a href="#ofDays-int-days" class="headerlink" title="ofDays(int days)"></a>ofDays(int days)</h4><h4 id="ofMonths-int-months"><a href="#ofMonths-int-months" class="headerlink" title="ofMonths(int months)"></a>ofMonths(int months)</h4><h4 id="ofWeeks-int-weeks"><a href="#ofWeeks-int-weeks" class="headerlink" title="ofWeeks(int weeks)"></a>ofWeeks(int weeks)</h4><h4 id="ofYears-int-years"><a href="#ofYears-int-years" class="headerlink" title="ofYears(int years)"></a>ofYears(int years)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* of(int years, int months, int days)</span></span><br><span class="line"><span class="comment">* ofDays(int days)</span></span><br><span class="line"><span class="comment">* ofMonths(int months)</span></span><br><span class="line"><span class="comment">* ofWeeks(int weeks)</span></span><br><span class="line"><span class="comment">* ofYears(int years)</span></span><br><span class="line"><span class="comment">* 作用：指定时间长度创建period对象。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test03</span><span class="params">()</span>&#123;</span><br><span class="line">    System.out.println(Period.of(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)); <span class="comment">// 输出 P1Y2M3D</span></span><br><span class="line">    System.out.println(Period.ofYears(<span class="number">1</span>)); <span class="comment">// 输出 P1Y</span></span><br><span class="line">    System.out.println(Period.ofMonths(<span class="number">2</span>)); <span class="comment">// 输出 P2M</span></span><br><span class="line">    System.out.println(Period.ofDays(<span class="number">3</span>)); <span class="comment">// 输出 P3D</span></span><br><span class="line">    System.out.println(Period.ofWeeks(<span class="number">4</span>)); <span class="comment">// 输出 P28D</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="成员方法-1"><a href="#成员方法-1" class="headerlink" title="成员方法"></a>成员方法</h3><h4 id="addTo-Temporal-temporal-1"><a href="#addTo-Temporal-temporal-1" class="headerlink" title="addTo(Temporal temporal)"></a>addTo(Temporal temporal)</h4><h4 id="subtractFrom-Temporal-temporal-1"><a href="#subtractFrom-Temporal-temporal-1" class="headerlink" title="subtractFrom(Temporal temporal)"></a>subtractFrom(Temporal temporal)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* addTo(Temporal temporal)</span></span><br><span class="line"><span class="comment">* subtractFrom(Temporal temporal)</span></span><br><span class="line"><span class="comment">* 作用：在指定的时间对象上 加上/减去 时间长度。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period01</span> <span class="operator">=</span> Period.parse(<span class="string">&quot;P1Y2M3D&quot;</span>);</span><br><span class="line">    <span class="type">LocalDate</span> <span class="variable">date01</span> <span class="operator">=</span> LocalDate.parse(<span class="string">&quot;2023-10-10&quot;</span>);</span><br><span class="line">    <span class="type">LocalDate</span> <span class="variable">date02</span> <span class="operator">=</span> (LocalDate) period01.addTo(date01);</span><br><span class="line">    <span class="type">LocalDate</span> <span class="variable">date03</span> <span class="operator">=</span> (LocalDate) period01.subtractFrom(date01);</span><br><span class="line">    System.out.println(date02); <span class="comment">// 输出 2024-12-13</span></span><br><span class="line">    System.out.println(date03); <span class="comment">// 输出 2022-08-07</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="getUnits-1"><a href="#getUnits-1" class="headerlink" title="getUnits()"></a>getUnits()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* getUnits()</span></span><br><span class="line"><span class="comment">* 作用：获取period对象支持的时间单位。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period01</span> <span class="operator">=</span> Period.parse(<span class="string">&quot;P1Y2M3D&quot;</span>);</span><br><span class="line">    System.out.println(period01.getUnits()); <span class="comment">// 输出 [Years, Months, Days]</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="isNegative"><a href="#isNegative" class="headerlink" title="isNegative()"></a>isNegative()</h4><h4 id="isZero"><a href="#isZero" class="headerlink" title="isZero()"></a>isZero()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* isNegative()</span></span><br><span class="line"><span class="comment">* 作用：只要有年月日中有一个值为负数则为true。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* isZero()</span></span><br><span class="line"><span class="comment">* 作用：只有年月日都为零时才为true。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test03</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period01</span> <span class="operator">=</span> Period.parse(<span class="string">&quot;P1Y&quot;</span>);</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period02</span> <span class="operator">=</span> Period.parse(<span class="string">&quot;P-1Y&quot;</span>);</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period03</span> <span class="operator">=</span> Period.of(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">    System.out.println(period01.isNegative()); <span class="comment">// 输出 false</span></span><br><span class="line">    System.out.println(period02.isNegative()); <span class="comment">// 输出 true</span></span><br><span class="line">    System.out.println(period01.isZero()); <span class="comment">// 输出 false</span></span><br><span class="line">    System.out.println(period03.isZero()); <span class="comment">// 输出 true</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="multipliedBy-int-scalar"><a href="#multipliedBy-int-scalar" class="headerlink" title="multipliedBy(int scalar)"></a>multipliedBy(int scalar)</h4><h4 id="negated-1"><a href="#negated-1" class="headerlink" title="negated()"></a>negated()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* multipliedBy(int scalar)</span></span><br><span class="line"><span class="comment">* 作用：对years，months，days三个属性值都乘以给定值。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* negated()</span></span><br><span class="line"><span class="comment">* 作用：返回period对象的相反数。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test04</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period01</span> <span class="operator">=</span> Period.parse(<span class="string">&quot;P1Y2M3D&quot;</span>);</span><br><span class="line">    System.out.println(period01.multipliedBy(<span class="number">2</span>)); <span class="comment">// 输出 P2Y4M6D</span></span><br><span class="line">    System.out.println(period01.negated()); <span class="comment">// 输出 P-1Y-2M-3D</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="normalized"><a href="#normalized" class="headerlink" title="normalized()"></a>normalized()</h4><h4 id="toTotalMonths"><a href="#toTotalMonths" class="headerlink" title="toTotalMonths()"></a>toTotalMonths()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* normalized()</span></span><br><span class="line"><span class="comment">* 作用：标准化period对象。如：</span></span><br><span class="line"><span class="comment">*      1年固定有12个月，对于P0Y13M1D的情况，调用normalized()可以标准化为P1Y1M1D。</span></span><br><span class="line"><span class="comment">*      1个月的天数是不固定的，所以对于P365D的情况是无法标准化。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* toTotalMonths()</span></span><br><span class="line"><span class="comment">* 作用：将period对象的时间长度用月份表示，返回值为 years * 12L + months。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test05</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period01</span> <span class="operator">=</span> Period.parse(<span class="string">&quot;P0Y13M1D&quot;</span>);</span><br><span class="line">    <span class="type">Period</span> <span class="variable">period02</span> <span class="operator">=</span> Period.parse(<span class="string">&quot;P1Y1M1D&quot;</span>);</span><br><span class="line">    System.out.println(period01.normalized()); <span class="comment">// 输出 P1Y1M1D</span></span><br><span class="line">    System.out.println(period02.toTotalMonths()); <span class="comment">// 输出 13</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="未演示方法-1"><a href="#未演示方法-1" class="headerlink" title="未演示方法"></a>未演示方法</h4><p>以下方法大多在 <code>LocalDate</code> 和 <code>LocalTime</code> 篇章演示过且作用类似，该篇章就不在赘述了。</p>
<ol>
<li>equals(Object otherDuration)</li>
<li>get(TemporalUnit unit)</li>
<li>getDays()</li>
<li>getSeconds()</li>
<li>getMonths()</li>
<li>getYears()</li>
<li>hashCode()</li>
<li>minus(TemporalAmount amountToSubtract)</li>
<li>minusDays(long daysToSubtract)</li>
<li>minusMonths(long monthsToSubtract)</li>
<li>minusYears(long yearsToSubtract)</li>
<li>plus(TemporalAmount amountToAdd)</li>
<li>plusDays(long daysToAdd)</li>
<li>plusMonths(long monthsToAdd)</li>
<li>plusYears(long yearsToAdd)</li>
<li>toString()</li>
<li>withDays(int days)</li>
<li>withMonths(int months)</li>
<li>withYears(int years)</li>
</ol>
<h2 id="参考文献"><a href="#参考文献" class="headerlink" title="参考文献"></a>参考文献</h2><p><a target="_blank" rel="noopener" href="https://www.matools.com/api/java8">https://www.matools.com/api/java8</a></p>
</div>
        </div>
        
        <footer class="kratos-entry-footer clearfix">
            
            <div class="footer-tag clearfix">
                <div class="pull-left">
                <i class="fa fa-tags"></i>
                    <a class="tag-none-link" href="/blog/tags/Java/" rel="tag">Java</a>
                </div>
                <div class="pull-date">
                    <time datetime="2025-11-20T11:35:22.171Z" itemprop="dateModified">最后编辑：2025-11-20</time>
                </div>
            </div>
        </footer>
    </div>
    
        <nav class="navigation post-navigation clearfix" role="navigation">
            
            <div class="nav-previous clearfix">
                <a title=" 【Java 8 新特性】时间类：Instant" href="/blog/2023/05/10/Java8/time/Instant/">&lt; 上一篇</a>
            </div>
            
            
            <div class="nav-next clearfix">
                <a title=" Idea 常用快捷键" href="/blog/2023/05/10/Idea/IDEA常用快捷键/">下一篇 &gt;</a>
            </div>
            
        </nav>
    
    
</article>

        

            </section>

        

                
            

<section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
    <!-- 文章和页面根据splitter来分割，没有的话就从头开始设置为sticky -->
    
    
                <aside id="krw-about" class="widget widget-kratos-about clearfix">
    <div class="photo-background"></div>
    <div class="photo-wrapper clearfix">
        <div class="photo-wrapper-tip text-center">
            <img class="about-photo" src="/blog/images/avatar.webp" loading="lazy" decoding="auto" />
        </div>
    </div>
    <div class="textwidget">
        <p class="text-center">极简主义</p>
    </div>
    <div class="site-meta">
        <a class="meta-item" href="/blog/archives/">
            <span class="title">
                文章
            </span>
            <span class="count">
                72
            </span>
        </a>
        <a class="meta-item" href="/blog/categories/">
            <span class="title">
                分类
            </span>
            <span class="count">
                9
            </span>
        </a>
        <a class="meta-item" href="/blog/tags/">
            <span class="title">
                标签
            </span>
            <span class="count">
                27
            </span>
        </a>
    </div>
</aside>
            
                    <div class="sticky-area">
                
                    <aside id="krw-toc" class="widget widget-kratos-toc clearfix toc-div-class" >
    <div class="photo-background"></div>
    <h4 class="widget-title no-after">
        <i class="fa fa-compass"></i>
        文章目录
        <span class="toc-progress-bar" role="progressbar" aria-label="阅读进度："></span>
    </h4>
    <div class="textwidget">
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Duration"><span class="toc-text">Duration</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AC%E5%85%B1%E5%B8%B8%E9%87%8F"><span class="toc-text">公共常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95"><span class="toc-text">静态方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#parse-CharSequence-text"><span class="toc-text">parse(CharSequence text)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#between-Temporal-startInclusive-Temporal-endExclusive"><span class="toc-text">between(Temporal startInclusive, Temporal endExclusive)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-long-amount-TemporalUnit-unit"><span class="toc-text">of(long amount, TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofDays-long-days"><span class="toc-text">ofDays(long days)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofHours-long-hours"><span class="toc-text">ofHours(long hours)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofMinutes-long-minutes"><span class="toc-text">ofMinutes(long minutes)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofSeconds-long-seconds"><span class="toc-text">ofSeconds(long seconds)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofSeconds-long-seconds-long-nanoAdjustment"><span class="toc-text">ofSeconds(long seconds, long nanoAdjustment)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofMillis-long-millis"><span class="toc-text">ofMillis(long millis)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofNanos-long-nanos"><span class="toc-text">ofNanos(long nanos)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%88%90%E5%91%98%E6%96%B9%E6%B3%95"><span class="toc-text">成员方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#abs"><span class="toc-text">abs()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#negated"><span class="toc-text">negated()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#addTo-Temporal-temporal"><span class="toc-text">addTo(Temporal temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#subtractFrom-Temporal-temporal"><span class="toc-text">subtractFrom(Temporal temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#compareTo-Duration-otherDuration"><span class="toc-text">compareTo(Duration otherDuration)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#multipliedBy-long-multiplicand"><span class="toc-text">multipliedBy(long multiplicand)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dividedBy-long-divisor"><span class="toc-text">dividedBy(long divisor)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getUnits"><span class="toc-text">getUnits()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%AA%E6%BC%94%E7%A4%BA%E6%96%B9%E6%B3%95"><span class="toc-text">未演示方法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Period"><span class="toc-text">Period</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AC%E5%85%B1%E5%B8%B8%E9%87%8F-1"><span class="toc-text">公共常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95-1"><span class="toc-text">静态方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#parse-CharSequence-text-1"><span class="toc-text">parse(CharSequence text)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#between-LocalDate-startDateInclusive-LocalDate-endDateExclusive"><span class="toc-text">between(LocalDate startDateInclusive, LocalDate endDateExclusive)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-int-years-int-months-int-days"><span class="toc-text">of(int years, int months, int days)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofDays-int-days"><span class="toc-text">ofDays(int days)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofMonths-int-months"><span class="toc-text">ofMonths(int months)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofWeeks-int-weeks"><span class="toc-text">ofWeeks(int weeks)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofYears-int-years"><span class="toc-text">ofYears(int years)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%88%90%E5%91%98%E6%96%B9%E6%B3%95-1"><span class="toc-text">成员方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#addTo-Temporal-temporal-1"><span class="toc-text">addTo(Temporal temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#subtractFrom-Temporal-temporal-1"><span class="toc-text">subtractFrom(Temporal temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getUnits-1"><span class="toc-text">getUnits()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isNegative"><span class="toc-text">isNegative()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isZero"><span class="toc-text">isZero()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#multipliedBy-int-scalar"><span class="toc-text">multipliedBy(int scalar)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#negated-1"><span class="toc-text">negated()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#normalized"><span class="toc-text">normalized()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#toTotalMonths"><span class="toc-text">toTotalMonths()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%AA%E6%BC%94%E7%A4%BA%E6%96%B9%E6%B3%95-1"><span class="toc-text">未演示方法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE"><span class="toc-text">参考文献</span></a></li></ol>
    </div>
</aside>
                
                
  <aside id="krw-categories" class="widget widget-kratos-categories clearfix">
    <h4 class="widget-title"><i class="fa fa-folder"></i>分类目录</h4>
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Docker/">Docker</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Idea/">Idea</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Java/">Java</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Linux/">Linux</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/MySQL/">MySQL</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Python/">Python</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Selenium/">Selenium</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/SpringBoot/">SpringBoot</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/SpringCloud/">SpringCloud</a><span class="category-list-count">13</span></li></ul>
  </aside>


            
                
  <aside id="krw-tags" class="widget widget-kratos-tags clearfix">
    <h4 class="widget-title"><i class="fa fa-tags"></i>标签聚合</h4>
      <div class="tag-clouds">
        <a href="/blog/tags/CORS/" style="font-size: 0.8em;">CORS</a> <a href="/blog/tags/Docker/" style="font-size: 0.8em;">Docker</a> <a href="/blog/tags/Dubbo/" style="font-size: 0.8em;">Dubbo</a> <a href="/blog/tags/ELK/" style="font-size: 0.8em;">ELK</a> <a href="/blog/tags/Elasticsearch/" style="font-size: 0.8em;">Elasticsearch</a> <a href="/blog/tags/Eureka/" style="font-size: 0.8em;">Eureka</a> <a href="/blog/tags/Feign/" style="font-size: 0.8em;">Feign</a> <a href="/blog/tags/Gateway/" style="font-size: 0.8em;">Gateway</a> <a href="/blog/tags/Idea/" style="font-size: 0.8em;">Idea</a> <a href="/blog/tags/Java/" style="font-size: 0.8em;">Java</a> <a href="/blog/tags/JavaWeb/" style="font-size: 0.8em;">JavaWeb</a> <a href="/blog/tags/Linux/" style="font-size: 0.8em;">Linux</a> <a href="/blog/tags/Maven/" style="font-size: 0.8em;">Maven</a> <a href="/blog/tags/MySQL/" style="font-size: 0.8em;">MySQL</a> <a href="/blog/tags/Mybatis/" style="font-size: 0.8em;">Mybatis</a> <a href="/blog/tags/Nacos/" style="font-size: 0.8em;">Nacos</a> <a href="/blog/tags/Pytest/" style="font-size: 0.8em;">Pytest</a> <a href="/blog/tags/Python/" style="font-size: 0.8em;">Python</a>
      </div>
  </aside>

            
                
  <aside id="krw-posts" class="widget widget-kratos-posts">
  <h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
  <div class="tab-content">
      <ul class="list-group">
        
        
          
          
            <a class="list-group-item" href="/blog/2024/04/07/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/"><i class="fa  fa-book"></i> 【Java】常见的日志框架</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/06/SpringCloud/ElasticStack/Elasticsearch%E6%90%9C%E7%B4%A2%E8%AF%AD%E6%B3%95/"><i class="fa  fa-book"></i> 【ES】搜索语法</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/03/SpringCloud/ElasticStack/Elasticsearch%E7%B4%A2%E5%BC%95%E9%85%8D%E7%BD%AE/"><i class="fa  fa-book"></i> 【ES】索引配置</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/02/SpringCloud/ElasticStack/Kibana%E5%B8%B8%E7%94%A8%E5%8A%9F%E8%83%BD/"><i class="fa  fa-book"></i> 【Kibana】常用功能</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/02/SpringCloud/ElasticStack/IK%E5%88%86%E8%AF%8D%E5%99%A8/"><i class="fa  fa-book"></i> 【IK 分词器】基本使用</a>
            
          
        
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
      </ul>
  </div>
  </aside>

            
    </div>
</section>
        
        </div>
    </div>
</div>
<footer>
    <div id="footer"  >
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 footer-list text-center">
                    <ul class="kratos-social-icons">
                        <!-- Keep for compatibility -->
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        <!-- New links -->
                        
                    </ul>
                    <ul class="kratos-copyright">
                        <div>
                            <li>&copy; 2025 溪岚花的博客 版权所有.</li>
                            <li>本站已运行<span id="span_dt">Loading...</span></li>
                        </div>
                        <div>
                            <li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
                            <li>Site built with&nbsp;<i class="fa fa-heart throb" style="color:#d43f57"></i>&nbsp;by 林家隆.</li>
                        </div>
                        <div>
                            <li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
                            <li>Hosted on <a href="https://gitee.com/" target="_blank">Gitee Pages</a></li>
                        </div>
                        <div>
                            
                            
                        </div>
                    </ul>
                </div>
            </div>
        </div>
        <div class="kr-tool text-center">
            <div class="tool">
                
                    <div class="box search-box">
                        <a href="/blog/search/">
                            <span class="fa fa-search"></span>
                        </a>
                    </div>
                
                
                    <div class="box theme-box" id="darkmode-switch">
                        <span class="fa fa-adjust"></span>
                    </div>
                
                
                
            </div>
            <div class="box gotop-box">
                <span class="fa fa-chevron-up"></span>
            </div>
        </div>
    </div>
</footer>
</div>
</div>

        <script defer src="/blog/vendors/bootstrap@3.3.4/dist/js/bootstrap.min.js"></script>
<script defer src="/blog/vendors/nprogress@0.2.0/nprogress.js"></script>
<script>
    if (!window.kr) {
        window.kr = {};
    }
    window.kr.notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));
    window.kr.siteRoot = "/blog/";
</script>





    <script defer src="/blog/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script defer src="/blog/js/kratosr.min.js"></script>
<script defer src="/blog/js/pjax.min.js"></script>



<!-- Extra support for third-party plguins  -->


    </body>
</html>